from PySide6.QtGui import QResizeEvent
from qfluentwidgets import InfoBar, StateToolTip
from ..base import WordFrameBase, ReturnView
from common import WordToPdfThread


class WordToPdfFrame(ReturnView):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.wordFrame = WordFrameBase(self)
        self.wordFrame.setTitle('Word 转 PDF')

        self.vBox.addWidget(self.returnButton)
        self.vBox.addWidget(self.wordFrame)

        self.stateToolTip = StateToolTip('Word转Pdf', '正在进行word转pdf，请稍后...', parent=self)
        self.stateToolTip.hide()
        self.vBox.addWidget(self.wordFrame)

        self.runThread = WordToPdfThread(self)

        def thread_error(content):
            InfoBar.error('错误', content, parent=self)

        def finished_thread():
            self.stateToolTip.hide()
            InfoBar.success('提示', '文件转换完毕', duration=3000, parent=self)

        self.runThread.started.connect(self.stateToolTip.show)
        self.runThread.error.connect(thread_error)
        self.runThread.finished.connect(finished_thread)

        self.wordFrame.started.connect(self.startThread)
        self.returnButton.clicked.connect(self.return_event)

    def startThread(self):
        openFile, saveFile = self.wordFrame.lineEditText()
        if openFile is False:
            return
        self.runThread.set_value(openFile, saveFile)
        self.runThread.start()

    def resizeEvent(self, a0: QResizeEvent):
        self.stateToolTip.move(a0.size().width() - 10 - self.stateToolTip.width(), 20)
        super().resizeEvent(a0)

    def return_event(self):
        self.wordFrame.clear()
        self.returned.emit()
